<?php

class User_Model extends Model {

    public function __construct() {
        parent::__construct();
    }

    public function get()
    {
        return json_encode( $this->db->select('SELECT idUser, userName, firstName, lastName, email
                                  FROM user ORDER BY userName'));
    }

    public function find($id) {
        $data = $this->db->select('SELECT idUser, userName, firstName, lastName, email FROM user WHERE idUser = :id', array(':id' => $id));
        return (isset($data[0])?$data[0]:null);
    }

    public function create() {
        $form = new Form();
        try {
            $form   ->post('userName')
                        ->val('minlength', 3)
                        ->val('maxlength', 16)
                        ->val('alnum')
                    ->post('password')
                        ->val('minlength', 6)
                        ->val('maxlength', 16)
                        ->val('alnum')
                    ->post('firstName')
                        ->val('minlength', 2)
                        ->val('maxlength', 22)
                        ->val('alnum')
                    ->post('lastName')
                        ->val('minlength', 2)
                        ->val('maxlength', 22)
                        ->val('alnum')
                    ->post('email')
                        ->val('email')
                    ->submit();
        } catch (Exception $e) {
            return $e->getMessage();
        }
        $data = $form->fetch();
        $data['password'] = Hash::create('sha256', $data['password'], HASH_PASSWORD_KEY);

        $this->db->insert('user', $data);
    }

    public function editSave($id) {
        $form = new Form();
        try {
            $form->post('firstName')
                    ->post('lastName')
                    ->post('email');
        } catch (Exception $e) {
            return $e->getMessage();
        }
        $data = $form->fetch();
        $data['password'] = Hash::create('sha256', $data['password'], HASH_PASSWORD_KEY);
        $this->db->update('user', $data, "`idUser` = $id");
    }

    public function delete($id) {

        $this->db->delete('user', "idUser = '$id'");
    }

    public function search() {
        $form = new Form();
        try {
            $form->post('search');
        } catch (Exception $e) {
            return $e->getMessage();
        }
        $data = $form->fetch();
        $res = $this->db->select("SELECT idUser, userName, firstName, lastName, email, address, telephone 
                                        FROM user WHERE userName LIKE :search 
                                                     OR firstName LIKE :search 
                                                     OR lastName LIKE :search
                                                     OR email LIKE :search
                                                     OR address LIKE :search
                                                     OR telephone LIKE :search", array(':search' => '%' . $data['search'] . '%'));
        return $res;
    }

}